package com.ruoyi.basebill.service.common;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.exception.DataNullException;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.basebill.util.OmsUtil;
import com.ruoyi.basedata.domain.BdCustomer;
import com.ruoyi.basedata.domain.BdSupplier;
import com.ruoyi.basedata.domain.OmsOrgunit;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.service.common
 * @className: AbstractPushSaleReturn
 * @author: Eric
 * @description: TODO
 * @date: 2023/2/22 15:12
 * @version: 1.0
 */
public abstract class AbstractPushSaleReturn {

    @Autowired
    private OmsTaskUtil omsTaskUtil;

    @Autowired
    private OmsUtil omsUtil;

    //生成销售出库（红字）
    public OmsSaleissuebill pushSaleIssue(OmsSalereturnbill omsSalereturnbill){
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        String currency = omsSalereturnbill.getCurrency();
        omsSaleissuebill.setBizdate(omsSalereturnbill.getCloseat());
        //币别
//        String s = omsTaskUtil.setBillNumber(omsSaleissuebill);
//        omsSaleissuebill.setBillnumber(s);
//        omsSaleissuebill.setCurrency("CNY");

        //update by wyx 去原单币别 date:20230403
        omsSaleissuebill.setCurrency(currency);
        //来源单据
        omsSaleissuebill.setSrcbilltype("销售退货");
        omsSaleissuebill.setSrcbillnumber(omsSalereturnbill.getCode());
        omsSaleissuebill.setExchangerate(BigDecimal.ONE);
        //创建时间
        omsSaleissuebill.setCreatetime(new Date());
        //来源系统
        omsSaleissuebill.setSrcsys(omsSalereturnbill.getSrcsys());

        //设置分录数据
        ArrayList<OmsSaleissuebillentry> saleissuebillentries= new ArrayList<>();
        List<OmsSalereturnbillentry> omsSalereturnbillentryList = omsSalereturnbill.getOmsSalereturnbillentryList();
        for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList) {
            OmsSaleissuebillentry omsSaleissuebillentry = new OmsSaleissuebillentry();
            //物料
            String skucode = omsSalereturnbillentry.getSkucode();
            String skuname = omsSalereturnbillentry.getSkuname();
//            String customerorder = omsSalereturnbillentry.getCustomerorder();
            omsSaleissuebillentry.setMaterialnumber(skucode);
            omsSaleissuebillentry.setMaterialname(skuname);
            //计量单位
            String unit = omsSalereturnbillentry.getUnit();
            omsSaleissuebillentry.setUnitnumber(unit);
            //基本计量单位
            String baseunit = omsSalereturnbillentry.getBaseunit();
            omsSaleissuebillentry.setBaseunitnumber(baseunit);
            Long isgift = omsSalereturnbillentry.getIsgift();
            omsSaleissuebillentry.setIspresent(0L);
//            omsSaleissuebillentry.setCustomerOrderNo(customerorder);
            saleissuebillentries.add(omsSaleissuebillentry);
        }
        omsSaleissuebill.setOmsSaleissuebillentryList(saleissuebillentries);
        return omsSaleissuebill;
    }

    //库存调拨单
    public OmsStocktransferbill pushStockTransfer(OmsSalereturnbill omsSalereturnbill){
        OmsStocktransferbill omsStocktransferbill = new OmsStocktransferbill();

        Date tradecreatedat = omsSalereturnbill.getCloseat(); //业务日期
        omsStocktransferbill.setBizdate(tradecreatedat);
        String s = omsTaskUtil.setBillNumber(omsStocktransferbill);
        String currency = omsSalereturnbill.getCurrency();
        BigDecimal exchangerate = omsSalereturnbill.getExchangerate();
        String code = omsSalereturnbill.getCode();
        omsStocktransferbill.setBillnumber(s);

        omsStocktransferbill.setBiztypenumber("331");//固定值：331
        omsStocktransferbill.setBiztypename("跨仓库调拨（成本价）"); //固定值：跨仓库调拨（成本价）
        omsStocktransferbill.setCurrency(currency);
        omsStocktransferbill.setExchangerate(exchangerate); //汇率
        omsStocktransferbill.setSrcbilltype("销售退货");

        omsStocktransferbill.setSrcbillnumber(code);
        omsStocktransferbill.setCreatetime(new Date());
        //来源系统
        omsStocktransferbill.setSrcsys(omsSalereturnbill.getSrcsys());

        List<OmsSalereturnbillentry> omsSalereturnbillentryList = omsSalereturnbill.getOmsSalereturnbillentryList();
        ArrayList<OmsStocktransferbillentry> omsStocktransferbillentries = new ArrayList<>();
        for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList) {
            OmsStocktransferbillentry omsStocktransferbillentry = new OmsStocktransferbillentry();
            String skucode = omsSalereturnbillentry.getSkucode();
            String skuname = omsSalereturnbillentry.getSkuname();
            String baseunit = omsSalereturnbillentry.getBaseunit();
            Long isgift = omsSalereturnbillentry.getIsgift();

            omsStocktransferbillentry.setMaterialname(skuname);
            omsStocktransferbillentry.setMaterialnumber(skucode);
            omsStocktransferbillentry.setBaseunitnumber(baseunit);
            omsStocktransferbillentry.setUnitnumber(baseunit);
            omsStocktransferbillentry.setIssueplandate(tradecreatedat);
            omsStocktransferbillentry.setReceiptplandate(tradecreatedat);
            omsStocktransferbillentry.setIspresent(isgift);
            omsStocktransferbillentries.add(omsStocktransferbillentry);
        }
        omsStocktransferbill.setOmsStocktransferbillentryList(omsStocktransferbillentries);
        return omsStocktransferbill;
    }

    /**
     *
     * @param omsSalereturnbill
     * @param orgSup 上级组织
     * @param orgUnitCode 下级的组织
     * @param reason
     * @return
     */
    //采购入库单
    public OmsPurinwarehsbill pushPurInWarehs(OmsSalereturnbill omsSalereturnbill,  String orgUnitCode,String orgSup,StringBuffer reason) throws DataNullException {
        String currency = omsSalereturnbill.getCurrency();
        OmsPurinwarehsbill omsPurinwarehsbill = new OmsPurinwarehsbill();
        Date tradecreatedat = omsSalereturnbill.getCloseat();

        OmsOrgunit omsOrgunit = omsUtil.getOmsOrgunit(orgUnitCode);

        BdSupplier supplier = omsUtil.getSupplier(orgSup);
        if(supplier==null){
            reason.append("该组织"+orgSup+"供应商未在结算中心存在于结算中心");
            throw new DataNullException(reason.toString());
        }
        String suppliername = supplier.getSuppliername();
        String suppliernumber = supplier.getSuppliernumber();


        //设置业务日期
        omsPurinwarehsbill.setBizdate(tradecreatedat);
        //设置单据编号
        String PWbillNumber = omsTaskUtil.setBillNumber(omsPurinwarehsbill);
        omsPurinwarehsbill.setBillnumber(PWbillNumber);
        //设置库存组织
        omsPurinwarehsbill.setStorageorgnumber(orgUnitCode);
        omsPurinwarehsbill.setStorageorgname(omsOrgunit.getOrgname());
        //设置业务类型
        omsPurinwarehsbill.setBiztypenumber("111");
        omsPurinwarehsbill.setBiztypename("普通采购退货");
        //设置事务类型
        omsPurinwarehsbill.setTransactiontypenumber("005");
        omsPurinwarehsbill.setTransactiontypename("成本入库退货");

        //设置供应商
        omsPurinwarehsbill.setSuppliernumber(suppliernumber);
        omsPurinwarehsbill.setSuppliername(suppliername);
        //设置成本中心
        omsPurinwarehsbill.setCostcenternumber("1.91");
        omsPurinwarehsbill.setCostcentername("关联单位");
        //设置记账公司
        omsPurinwarehsbill.setAccountcompanynumber(orgUnitCode);
        omsPurinwarehsbill.setAccountcompanyname(omsOrgunit.getOrgname());
        //币别：update by wyx：取原单币别，date：2023-04-03
        omsPurinwarehsbill.setCurrency(currency);
        //汇率
        omsPurinwarehsbill.setExchangerate(BigDecimal.valueOf(1));
        //来源单据
        omsPurinwarehsbill.setSrcbilltype("销售退货");
        omsPurinwarehsbill.setSrcbillnumber(omsSalereturnbill.getCode());
        //创建时间
        omsPurinwarehsbill.setCreatetime(new Date());
        //来源系统
        omsPurinwarehsbill.setSrcsys(omsSalereturnbill.getSrcsys());

        ArrayList<OmsPurinwarehsbillentry> omsPurinwarehsbillentries = new ArrayList<>();
        List<OmsSalereturnbillentry> omsSalereturnbillentryList = omsSalereturnbill.getOmsSalereturnbillentryList();
        for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList) {
            OmsPurinwarehsbillentry omsPurinwarehsbillentry = new OmsPurinwarehsbillentry();
            String skucode = omsSalereturnbillentry.getSkucode();
            String skuname = omsSalereturnbillentry.getSkuname();
            String unit = omsSalereturnbillentry.getUnit();
            String baseunitnumber = omsSalereturnbillentry.getBaseunit();
            BigDecimal baseqty = omsSalereturnbillentry.getBaseqty();
            Long isgift = omsSalereturnbillentry.getIsgift();

            omsPurinwarehsbillentry.setMaterialnumber(skucode);
            omsPurinwarehsbillentry.setMaterialname(skuname);
            omsPurinwarehsbillentry.setUnitnumber(unit);
            omsPurinwarehsbillentry.setBaseunitnumber(baseunitnumber);
            omsPurinwarehsbillentry.setBaseqty(baseqty);
            omsPurinwarehsbillentry.setIspresent(0L);

            omsPurinwarehsbillentries.add(omsPurinwarehsbillentry);
        }
        omsPurinwarehsbill.setOmsPurinwarehsbillentryList(omsPurinwarehsbillentries);
        return omsPurinwarehsbill;
    }



}
